package Coffee.controller;

import Coffee.bean.Coffee;
import Coffee.bean.Order;
import Coffee.bean.User;
import Coffee.service.*;

import java.util.List;
import java.util.Scanner;
import java.util.UUID;

public class hua {
    public static String getInput(Scanner sc, String prompt) {
        System.out.println(prompt);
        //静态通用方法，接收String
        return sc.next();
    }

    public static int getInputInt(Scanner sc, String prompt) {
        System.out.println(prompt);
        //静态通用方法，接收int
        return sc.nextInt();
    }


    public static void main(String[] args) {
        System.out.println("-----------------------------------------");
        System.out.println("---------------咖啡厅---------------------");
        System.out.println("-----------------------------------------");
        UserService userService = new UserServiceImpl();
        CoffeeService coffeeService = new CoffeeServiceImpl();
        OrderService orderService = new OrderServiceImpl();
        Scanner sc = new Scanner(System.in);

        boolean loginRight = true;
        while (loginRight) {
            int choose1 = getInputInt(sc, "请选择登录1 或注册2");
            if (choose1 == 1) {
                System.out.println("------登录-------------");
                String username = getInput(sc, "请输入用户名");
                String password = getInput(sc, "请输入密码");
                boolean login = userService.login(username, password);
                loginRight = !login;//查询的到则将loginRight赋反值
            } else {
                System.out.println("----------注册----------");
                String username = getInput(sc, "请输入新用户名:");
                String password = getInput(sc, "请输入密码:");
                User user = new User(UUID.randomUUID().toString().replaceAll("-", ""), username, password, 0);
                userService.insertUser(user);
            }
        }
        /////////////主界面///////////////
        boolean running = true;
        while (running) {//运行
            int i = getInputInt(sc, "请输入数字去选择角色: 1是顾客 2是管理员 3是退出");
            if (i == 3) {
                running = false;
                System.out.println("欢迎下次使用！");
            }
            boolean customer = true;
            if (i == 1) {
                String staticName = getInput(sc, "请选择执行操作的用户名");//输入的用户名执行下面所有的操作
                User user = userService.queryUser(staticName);
                if (user == null) {
                    System.out.println("用户不存在，重新选择角色");
                    continue;
                }
                while (customer) {
                    //普通用户的操作
                    System.out.printf("查看菜单输入1: %s 搜索饮品输入2 %s 下单请输入3: %s 查看订单输入4: %s 退出输入5%s 查看余额输入6：%s 充值输入7：%n ", "", "", "", "", "", "");
                    int u1 = sc.nextInt();
                    switch (u1) {
                        case 5:
                            customer = false;
                            System.out.println("感谢惠顾");
                            break;
                        case 1:
                            List<Coffee> coffees = coffeeService.selectAll();
                            coffees.iterator().forEachRemaining(System.out::println);
                            break;
                        case 2:
                            String search = getInput(sc, "请输入想要的饮品名");
                            List<Coffee> cofLike = coffeeService.selectlikaname(search);
                            cofLike.iterator().forEachRemaining(System.out::println);
                            break;
                        case 3:
                            String cof = getInput(sc, "请输入饮品");
                            int count = getInputInt(sc, "请输入数量");
                            if(coffeeService.selectbyname(cof)==null){
                                continue;
                            }
                            orderService.insertOrder(staticName, cof, count);
                            String choosePay = getInput(sc, "是否现在支付Y/N");
                            if (choosePay.equalsIgnoreCase("Y")) {
                                List<Order> unpaidOrders = orderService.queryOrderByState(staticName, "待支付");
                                unpaidOrders.iterator().forEachRemaining(System.out::println);
                                int orderIndex = getInputInt(sc, "请选择要支付的订单,从上往下12345排index");
                                Order order = unpaidOrders.get(orderIndex - 1);
                                userService.payOrder(order.getOrderid(), staticName);
                            }
                            break;
                        case 4:
                            boolean userOrder = true;
                            while (userOrder) {
                                int u2 = getInputInt(sc, "查看所有订单输入1： 查看指定状态订单输入2： 支付输入3： 退单输入4： 拿货输入5： 返回输入6");
                                switch (u2) {//订单状态：待支付、制作中、已完成、已取消
                                    case 6:
                                        userOrder = false;
                                        break;
                                    case 1:
                                        List<Order> orders = orderService.queryOrder(staticName);
                                        orders.iterator().forEachRemaining(System.out::println);
                                        break;
                                    case 2:
                                        String orderState = getInput(sc, "请输入订单状态");
                                        List<Order> orderStateList = orderService.queryOrderByState(staticName, orderState);
                                        orderStateList.iterator().forEachRemaining(System.out::println);
                                        break;
                                    case 3:
                                        //支付,先展示所有的未支付的订单
                                        List<Order> unpaidOrders = orderService.queryOrderByState(staticName, "待支付");
                                        unpaidOrders.iterator().forEachRemaining(System.out::println);
                                        int orderIndex = getInputInt(sc, "请选择要支付的订单,从上往下12345排index");
                                        Order order = unpaidOrders.get(orderIndex - 1);
                                        userService.payOrder(order.getOrderid(), staticName);
                                        break;
                                    case 4:
                                        //退单,展示改用户的所有订单
                                        List<Order> unfinishedOrders = orderService.queryOrder(staticName);
                                        unfinishedOrders.iterator().forEachRemaining(System.out::println);
                                        int orderIndex1 = getInputInt(sc, "请选择要退单的订单,从上往下12345排index");
                                        Order order1 = unfinishedOrders.get(orderIndex1 - 1);
                                        userService.repay(order1.getOrderid());
                                        break;
                                    case 5:
                                        //拿货,前提已经拿到筛选的订单表（制作中）
                                        List<Order> makingOrders = orderService.queryOrderByState(staticName, "制作中");
                                        makingOrders.iterator().forEachRemaining(System.out::println);
                                        int orderIndex2 = getInputInt(sc, "请选择要拿货的订单,从上往下12345排index");
                                        Order order2 = makingOrders.get(orderIndex2 - 1);
                                        userService.getCoffee(order2.getOrderid());
                                        break;
                                }//选择
                            }//用户订单
                            break;
                        case 6:
                            userService.queryMoneyByUsername(staticName);
                            break;
                        case 7:
                            int money = getInputInt(sc, "请输入充值金额");
                            userService.insertMoney(money, staticName);
                            break;
                    }
                }//维持
            }//普通用户操作
            boolean admin = true;
            if (i == 2) {
                while (admin) {
                    //管理员的操作
                    //增删改查饮品，查询所有用户，搜索用户，更改信息，查看所有订单，查看指定订单，
                    int u1 = getInputInt(sc, "饮品管理输入1  订单管理输入2  用户管理请输入3  退出输入4");
                    switch (u1) {
                        case 4:
                            admin = false;
                            System.out.println("感谢惠顾");
                            break;
                        case 1:
                            boolean adminCof = true;
                            while (adminCof) {
                                System.out.println("------------------饮品管理---------------------------");
                                int ad1 = getInputInt(sc, "添加输入1     删除输入2   查询输入3   修改输入4   返回输入5");
                                switch (ad1) {
                                    case 5:
                                        adminCof = false;
                                        break;
                                    case 1:
                                        String cofName = getInput(sc, "输入添加的饮品名");
                                        int cofPrice = getInputInt(sc, "设置该价格");
                                        coffeeService.insertCoffee(new Coffee(UUID.randomUUID().toString().replaceAll("-", ""),
                                                cofName, cofPrice, "在售"));
                                        break;
                                    case 2:
                                        String delete = getInput(sc, "输入要删除的饮品名");
                                        System.out.println(coffeeService.delbyname(delete));
                                        break;
                                    case 3:
                                        List<Coffee> coffees = coffeeService.selectAll();
                                        coffees.iterator().forEachRemaining(System.out::println);
                                        break;
                                    case 4:
                                        String updateCof = getInput(sc, "输入要修改的饮品名");
                                        int adminUpdateCof = getInputInt(sc, "设置价格");
                                        String updateState = getInput(sc, "设置状态");
                                        System.out.println(coffeeService.updateCofbyname(updateCof, adminUpdateCof, updateState));
                                        break;
                                }
                            }//饮品管理
                            break;
                        case 2:
                            boolean adminOrder = true;
                            while (adminOrder) {
                                System.out.println("----------------订单管理---------------------------");
                                int adminOrderChoose1 = getInputInt(sc, "查看所有订单输入1，查看指定用户订单输入2，退出输入3");
                                switch (adminOrderChoose1) {
                                    case 3:
                                        adminOrder = false;
                                        break;
                                    case 1:
                                        List<Order> orders = orderService.queryOrderAll();
                                        orders.iterator().forEachRemaining(System.out::println);
                                        break;
                                    case 2:
                                        String adminOrderQuery = getInput(sc, "输入要查询的用户");
                                        List<Order> orders1 = orderService.queryOrder(adminOrderQuery);
                                        orders1.iterator().forEachRemaining(System.out::println);
                                        boolean OrderDoing = true;
                                        while (OrderDoing) {
                                            int OrderDo = getInputInt(sc, "查看指定状态订单输入1，修改订单状态输入2，返回输入3");
                                            switch (OrderDo) {
                                                case 1:
                                                    String OrderStateAdmin = getInput(sc, "输入要查询的状态");
                                                    List<Order> orders2 = orderService.queryOrderByState(adminOrderQuery, OrderStateAdmin);
                                                    orders2.iterator().forEachRemaining(System.out::println);
                                                    break;
                                                case 2:
                                                    List<Order> orders3 = orderService.queryOrderAdmin(adminOrderQuery);
                                                    orders3.iterator().forEachRemaining(System.out::println);
                                                    int orderChoose = getInputInt(sc, "请输入要修改的订单index");
                                                    String orderUpdate = getInput(sc, "输入修改状态");
                                                    orderService.updateOrderState(orderUpdate, orders3.get(orderChoose - 1).getOrderid());
                                                    break;
                                                case 3:
                                                    OrderDoing = false;
                                                    break;
                                            }
                                        }
                                }
                            }
                        case 3:
                            boolean adminUser = true;
                            while (adminUser) {
                                System.out.println("--------------用户管理-----------------");
                                int ad2 = getInputInt(sc, "添加输入1     删除输入2   查询输入3   修改输入4   返回输入5");
                                switch (ad2) {
                                    case 5:
                                        adminUser = false;
                                        break;
                                    case 1:
                                        String username = getInput(sc, "请输入添加用户名:");
                                        String password = getInput(sc, "请设置其密码:");
                                        userService.insertUser(new User(UUID.randomUUID().toString().replaceAll("-", ""),
                                                username, password, 0));
                                        break;
                                    case 2:
                                        String adminDel = getInput(sc, "请输入要删除的用户用户名");
                                        userService.delUserbyName(adminDel);
                                        break;
                                    case 3:
                                        List<User> users = userService.queryAll();
                                        users.iterator().forEachRemaining(System.out::println);
                                        String adminQuery = getInput(sc, "请输入查询的用户名");
                                        userService.queryUser(adminQuery);
                                        break;
                                    case 4:
                                        String updateAdminOld = getInput(sc, "请输入要修改的账户");
                                        String updateAdminNew = getInput(sc, "请输入新的账户名");
                                        String updateAdminUserPwd = getInput(sc, "请输入新的密码");
                                        System.out.println(userService.updateUser(updateAdminOld, updateAdminNew, updateAdminUserPwd));
                                        break;
                                }
                            }//用户管理
                    }
                }//维持
            }//管理员操作
        }//运行
    }//main
}//类

